package com.zmx.user.web;

import com.zmx.common.common.web.AbstractController;
import com.zmx.common.common.web.rest.ResponseResult;
import com.zmx.user.dto.MemberCartDto;
import com.zmx.user.service.MemberCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 购物车控制器
 * @ClassName: MemberCartController
 * @Author zhaomxr
 * @Date 2022-01-11 10:01
 */
@Api("购物车")
@Slf4j
@RestController
@RequestMapping("cart")
public class MemberCartController extends AbstractController<MemberCartDto, MemberCartService> {

    @Autowired
    private MemberCartService memberCartService;

    @ApiOperation(value = "获取用户购物车", tags = "获取用户购物车")
    @GetMapping(value = "getCart")
    public ResponseResult getCart(@RequestParam(required = true) Integer pageIndex, @RequestParam(required = true) Integer pageSize) {
        log.info("begin to getCart pageIndex: {} pageSize: {}", pageIndex, pageSize);
        try {
            Page page = memberCartService.getCart(pageIndex, pageSize);
            return ResponseResult.success(page);
        } catch (Exception e) {
            log.error("获取购物车失败！", e);
            return ResponseResult.fail("获取购物车失败");
        }
    }

    @ApiOperation(value = "加入购物车", tags = "加入购物车")
    @PostMapping(value = "addCart")
    public ResponseResult addCart(@RequestParam(required = true) Long productId, @RequestParam(required = true) int quantity) {
        log.info("begin to addCart productId: {} quantity: {}", productId, quantity);
        try {
            memberCartService.addCart(productId, quantity);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("加入购物车失败！", e);
            return ResponseResult.fail("加入购物车失败");
        }
    }

    @ApiOperation(value = "删除购物车商品", tags = "删除购物车商品")
    @PostMapping(value = "deleteCart")
    public ResponseResult deleteCart(@RequestParam(required = true) Long[] productIds) {
        log.info("begin to deleteCart productIds: {}" + productIds);
        try {
            memberCartService.deleteCart(productIds);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("删除购物车商品失败！", e);
            return ResponseResult.fail("删除购物车商品失败");
        }
    }

    @ApiOperation(value = "更新购物车", tags = "更新购物车")
    @PostMapping(value = "updateCart")
    public Object updateCart(@RequestParam(required = true) Long productId, @RequestParam(required = true) int quantity) {
        log.info("begin to updateCart productId: {} quantity: {}", productId, quantity);
        try {
            memberCartService.updateCart(productId, quantity);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("更新购物车失败！", e);
            return ResponseResult.fail("更新购物车失败");
        }
    }
}
